package com.mxprojects.admin
{
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.constructors.ButtonSmall;
	import com.mxprojects.constructors.CheckboxConstructor;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.constructors.PopupWindow;
	import com.mxprojects.gui.ConfirmDialog;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.gui.ForumDropdown;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Posts;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class AdminThreads extends MovieClip
	{
		private const ADMIN_SCRIPT:String = "admin-frontend.php";
		
		private var forumID:int;
		private var threadID:int;
		private var threadTitle:String;
		private var forumDropdown:ForumDropdown;
		private var adminButton:ButtonLarge;
		private var popup:PopupWindow;
		private var holder:Sprite;
		private var editSubject:InputConstructor;
		private var resetSubjectButton:ButtonSmall;
		private var deleteThreadButton:ButtonLarge;
		private var saveButton:ButtonLarge;
		private var annoucementCheck:CheckboxConstructor;
		private var stickyCheck:CheckboxConstructor;
		private var lockedCheck:CheckboxConstructor;
		private var isSticky:Boolean;
		private var isLocked:Boolean;
		private var isAnnoucement:Boolean;
		private var currentPage:int;
		
		public function AdminThreads(){}
		
		public function display(forumID:int, threadID:int, threadTitle:String, isSticky:Boolean, isLocked:Boolean, isAnnoucement:Boolean, currentPage:int):void {
			this.forumID = forumID;
			this.threadID = threadID;
			this.threadTitle = threadTitle;
			this.isSticky = isSticky;
			this.isLocked = isLocked;
			this.isAnnoucement = isAnnoucement;
			this.currentPage = currentPage;
			
			createAdminButton();
		}
		
		private function createAdminButton():void {
			//this will make a button that we can place anywhere to pop open the admin panel
			adminButton = new ButtonLarge("<span class=\"buttonAdminText\">" + Language.main(Word.THREAD_ADMIN) + "</span>", openPanel);
			//adminButton.background.transform.colorTransform = Styles.colorTransformCSS(Styles.colorToUint(".buttonAdminBackground"), ".5");
			addChild(adminButton);
		}
		
		private function openPanel(event:MouseEvent):void {
			adminButton.enabled = false;
			
			//open the popup if it's not open (it shouldn't be)
			if(popup == null){
				popup = new PopupWindow(Config.DEFAULT_POPUP_WIDTH, Config.DEFAULT_POPUP_HEIGHT, Language.main(Word.THREAD_ADMIN), closePanel);
				popup.extraTitle(threadTitle);
				popup.createActionButton(Language.main(Word.CLOSE), closePanel);
				
				//place everything in the popup window
				createContent();
			}
		}
		
		private function createContent():void {
			//this is responsible for everything inside the popup window. Magical.
			var nextY:int = 0;
			var nextX:int = 0;
			
			//make a sprite that holds all.
			holder = new Sprite();
			holder.x = Math.round(popup.titleText.x + Config.TEXT_PADDING);
			holder.y = Math.round(popup.titleText.y + popup.titleText.height + Config.TEXT_PADDING);
			
			//allow the subject to be changed and reset to what it was before the change
			editSubject = new InputConstructor(Language.main(Word.EDIT_SUBJECT), threadTitle, 1, false, 1, "threadAdminInputLabel", "threadAdminInput");
			nextX += editSubject.inputBox.x;
			nextY += editSubject.height + Config.TEXT_PADDING;
			holder.addChild(editSubject);
			
			resetSubjectButton = new ButtonSmall(Language.main(Word.RESET), resetSubject);
			resetSubjectButton.x = Math.round(editSubject.width + Config.TEXT_PADDING);
			resetSubjectButton.y = Math.round(editSubject.height/2 - resetSubjectButton.height/2);
			holder.addChild(resetSubjectButton);
			
			//start of the various checkboxes
			stickyCheck = new CheckboxConstructor(Language.main(Word.MAKE_THREAD_STICKY), Word.TRUE, isSticky);
			stickyCheck.x = nextX;
			stickyCheck.y = nextY;
			nextY += stickyCheck.height + Config.TEXT_PADDING;
			holder.addChild(stickyCheck);
			
			//lock thread
			lockedCheck = new CheckboxConstructor(Language.main(Word.LOCK_THREAD), Word.TRUE, isLocked);
			lockedCheck.x = nextX;
			lockedCheck.y = nextY;
			nextY += lockedCheck.height + Config.TEXT_PADDING;
			holder.addChild(lockedCheck);
			
			//make the thread an annoucement
			annoucementCheck = new CheckboxConstructor(Language.main(Word.MAKE_THREAD_ANNOUNCEMENT), Word.TRUE, isAnnoucement);
			annoucementCheck.x = nextX;
			annoucementCheck.y = nextY;
			nextY += annoucementCheck.height + Config.TEXT_PADDING;
			holder.addChild(annoucementCheck);
			
			//if they click the annoucement box then they can't make the thread sticky. Annoucements go over everything
			annoucementCheck.addEventListener(CheckboxConstructor.TOGGLE, toggleAnnoucement, false, 0, true);
			
			//add a save button
			saveButton = new ButtonLarge(Language.main(Word.SAVE), saveThread);
			saveButton.x = nextX;
			saveButton.y = nextY;
			nextY += saveButton.height + Config.TEXT_PADDING;
			holder.addChild(saveButton);
			
			//delete thread			
			deleteThreadButton = new ButtonLarge("<span class=\"buttonAdminText\">" + 
												 Language.main(Word.DELETE_THREAD) + "</span>", deleteThread);
			deleteThreadButton.x = Math.round(holder.width + (popup.width - holder.width)/2 - deleteThreadButton.width/2 - holder.x);
			deleteThreadButton.y = Math.round(holder.height/2 - deleteThreadButton.height/2);
			holder.addChild(deleteThreadButton);
			
			//move thread TODO: create a new dropdown component that supports MUCH more options
			/*
			var moveThread:SimpleTextField = new SimpleTextField("<span class=\"formInputLabel\">" + 
																  Language.main(Word.MOVE_THREAD) + InputConstructor.LABEL_SEPARATOR +
																  "</span>");
			moveThread.y = nextY;
			moveThread.border = true;
			holder.addChild(moveThread);
			
			forumDropdown = new ForumDropdown(forumID);
			forumDropdown.x = Math.round(moveThread.width);
			forumDropdown.y = Math.round(moveThread.y + (moveThread.height/2 - forumDropdown.height/2));
			nextY += forumDropdown.height + Config.TEXT_PADDING;
			holder.addChild(forumDropdown);
			*/
			
			//add the holder to the window
			popup.addContent(holder);
		}
		
		private function resetSubject(event:MouseEvent):void {
			//simply put the input text to the subject it previously was
			editSubject.inputBox.text = threadTitle;
		}
		
		private function toggleAnnoucement(event:CustomEvent):void {
			//this is when the annoucement toggle is checked or not
			var isChecked:Boolean = event.customParameter[0];
			
			if(isChecked){
				//disable the sticky checkbox
				stickyCheck.setCheckState(false);
				stickyCheck.enabled = false;
			}else{
				stickyCheck.enabled = true;
			}
		}
		
		private function saveThread(event:MouseEvent):void {			
			//take the current values and send them off to the script
			var dataToSend:Object = new Object();
			dataToSend.forumID = forumID;
			dataToSend.threadID = threadID;
			dataToSend.subject = editSubject.getValue();
			dataToSend.sticky = stickyCheck.getValueIfChecked();
			dataToSend.locked = lockedCheck.getValueIfChecked();
			dataToSend.annoucement = annoucementCheck.getValueIfChecked();
			dataToSend.action = AdminActions.SAVE_THREAD;
			
			//this will handle the admin common data transfer
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, updateSuccess, saveButton, ADMIN_SCRIPT);
		}
		
		private function updateSuccess(returnData:Object):void {
			//this is back from the server if everything went ok
			DefaultDialog.show(Language.main(Word.MESSAGE_INFO_SAVED), false, Word.SUCCESS);
			
			//reload the thread behind (have to not use deep linking to force refresh it)
			Posts.load(threadID, currentPage);
		}
		
		private function deleteThread(event:MouseEvent):void {
			//this will delete the thread and once it's done it'll popup a window saying all is well
			ConfirmDialog.confirm(Language.main(Word.DELETE_CONFIRM), Language.main(Word.DELETE, true), confirmDeleteThread, null, false);
		}
		
		private function confirmDeleteThread(event:MouseEvent = null):void {
			//this will send to the server a message to delete the thread right away.
			//delete the post for real
			var dataToSend:Object = new Object();
			dataToSend.forumID = forumID;
			dataToSend.threadID = threadID;
			dataToSend.action = AdminActions.DELETE_THREAD;
			
			//this will handle the admin common data transfer
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, deleteSuccess, deleteThreadButton, ADMIN_SCRIPT);
		}
		
		private function deleteSuccess(returnData:Object):void {
			//after deleting the thread
			DeepLinking.setLink(Word.THREADS, DeepLinking.createLinkArray(forumID));
			
			//close the panel and bring up a dialog saying that it was all good
			closePanel();
			
			DefaultDialog.show(Language.main(Word.MESSAGE_THREAD_DELETED), true, Word.SUCCESS);
		}
		
		private function closePanel(event:MouseEvent = null):void {
			//close up shop and bring back the button (if it's still around)
			if(adminButton != null){
				adminButton.enabled = true;
			}
			
			popup.removeFromStage();
			popup = null;
		}
	}
}